package com.tarena.lbs.basic.web.service;

import com.alibaba.nacos.api.utils.StringUtils;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.basic.web.repository.AdminRepository;

import com.tarena.lbs.basic.web.repository.UserRepository;
import com.tarena.lbs.common.passport.encoder.JwtEncoder;
import com.tarena.lbs.common.passport.enums.Roles;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.basic.bo.AdminBO;
import com.tarena.lbs.pojo.basic.bo.LoginBO;
import com.tarena.lbs.pojo.basic.bo.UserBO;
import com.tarena.lbs.pojo.passport.param.AdminLoginParam;
import com.tarena.lbs.pojo.passport.param.UserLoginParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class LoginService {
    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private JwtEncoder<UserPrinciple> jwtEncoder;

    public AdminBO adminDoLogin(AdminLoginParam param) throws BusinessException {
        //1、根据电话号码查询lbs_admin表格的user对象
        log.info("入参:{}",param);
        String phone = param.getPhone();
        AdminBO adminBO = adminRepository.getUserByPhone(phone);
        //2、校验是否存在
        if (adminBO == null || adminBO.getId() == null) {
            throw new BusinessException("100","用户名不存在");
        }
        //3、如果存在 校验密码是否相等
        matchPassword(param.getPassword(),adminBO.getAccountPassword());
        //4、如果校验都成功 生成JWT 封装查询adminBO返回
        generateJwt(adminBO);
        System.out.println("adminBO = " + adminBO);
        return adminBO;
    }

    private void generateJwt(LoginBO loginBO) throws BusinessException {
        //1.准备好 一个写入jwt的数据对象UserPrinciple 利用adminBO 封装
        UserPrinciple userPrinciple=new UserPrinciple();
        userPrinciple.setId(loginBO.getId());
        userPrinciple.setNickname(loginBO.getNickname());
        //2.根据adminBO中accountType的值 0 给角色Admin 1 给角色SHOP
        Integer roleNum = loginBO.getRoles();//0,1,2
        //TODO 代码瑕疵 if判断数字 提供role赋值 业务强耦合
        if (roleNum==0){
            userPrinciple.setRole(Roles.ADMIN);
        }else if (roleNum==1){
            userPrinciple.setRole(Roles.SHOP);
        }else {
            userPrinciple.setRole(Roles.USER);
        }
        //3.生成JWT 使用jwtEncoder
        String jwt = jwtEncoder.generateToken(userPrinciple);
        //4. adminBO 缺少
        loginBO.setJwt(jwt);
    }

    private void matchPassword(String password, String accountPassword) throws BusinessException {
        System.out.println("password = " + password + ", accountPassword = " + accountPassword);
        boolean b = StringUtils.equals(password,accountPassword);
        if (!b){
            throw new BusinessException("101","密码错误");
        }
    }
    @Autowired
    private UserRepository userRepository;
    public UserBO UserDoLogin(UserLoginParam param) throws BusinessException {
        //1.根据phone电话号 到lbs_user表格查询数据 select * from lbs_user where phone=#{phone}
        UserBO userBO=userRepository.getUserByPhone(param.getPhone());
        //2.判断存在 不存在抛异常
        if(userBO == null || userBO.getId() == null){
            throw new BusinessException("100","用户不存在");
        }
        //3.存在 对比密码 错误抛异常
        matchPassword(param.getPassword(),userBO.getPassword());
        //4.生成JWT 包装到bo 返回
        generateJwt(userBO);
        return userBO;
    }

    public UserBO userDetail() throws BusinessException {
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        if (userPrinciple==null){
            throw new BusinessException("101","用户解析异常");
        }
        Integer userId = userPrinciple.getId();
        UserBO userBO = userRepository.selectById(userId);
        return userBO;
    }
}
